﻿using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;

using Joyintech.Common;
using Joyintech.BLL.inpiao365;
using Joyintech.Model.inpiao365;
using System.Data;
using System.Collections;

namespace Joyintech.Admin.inpiao365.general.system.role
{
    public partial class edit : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                //菜单权限
                ArrayList menuArray = new ArrayList();

                //判断是否为修改操作
                if (DNTRequest.GetQueryString("role_id") != "")
                {
                    DataBindForm(out menuArray);
                }

                //绑定菜单
                DataBindTree("0", this.treeViewMenu.Nodes, menuArray);

            }
        }

        #region 绑定菜单树

        /// <summary>
        /// 绑定菜单树
        /// </summary>
        /// <param name="menuPID"></param>
        /// <param name="Nds"></param>
        private void DataBindTree(string menuPID, TreeNodeCollection Nds, ArrayList menuArray)
        {
            List<systemMenu> list = new List<systemMenu>();

            list = new systemMenu_BLL().GetModelList("menuPID=" + menuPID + " order by menuOrder");

            if (list != null && list.Count > 0)
            {
                foreach (systemMenu model in list)
                {
                    TreeNode treeNode = new TreeNode();
                    treeNode.Text = model.MenuName;
                    treeNode.Value = model.MenuId.ToString();
                    treeNode.ImageUrl = "/theme/read_0.png";
                    treeNode.NavigateUrl = "edit.aspx?menu_id=" + treeNode.Value;

                    if (menuArray.IndexOf(treeNode.Value) > -1)
                    {
                        treeNode.Checked = true;
                    }

                    Nds.Add(treeNode);

                    //递归查找子部门
                    DataBindTree(treeNode.Value, treeNode.ChildNodes, menuArray);
                }
            }
        }

        #endregion

        #region 绑定表单数据

        /// <summary>
        /// 绑定表单数据
        /// </summary>
        private void DataBindForm(out ArrayList menuArray)
        {
            menuArray = new ArrayList();

            DataSet ds = new DataSet();

            ds = new systemRole_BLL().GetList("roleId=" + DNTRequest.GetQueryString("role_id"));

            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                Joyintech.WebControls.HFPanel.BindForm(this.HFPanel1, ds.Tables[0].DefaultView);

                //绑定菜单权限
                string[] RoleFunCode = ds.Tables[0].Rows[0]["RoleFunCode"].ToString().Split(',');

                foreach (string menuId in RoleFunCode)
                {
                    menuArray.Add(menuId);
                }
            }
        }

        #endregion

        #region 保存表单数据

        /// <summary>
        /// 保存表单数据到表systemRole
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSave_Click(object sender, EventArgs e)
        {
            systemRole model = new systemRole();

            string strError = string.Empty;

            strError = Joyintech.WebControls.HFPanel.FormCheck(this.HFPanel1, model);

            if (!strError.Equals(""))
            {
                MessageBox.Show(this, strError);
                return;
            }

            Joyintech.WebControls.HFPanel.GetForm(this.HFPanel1, model);

            foreach (TreeNode treeNode in this.treeViewMenu.CheckedNodes)
            {
                model.RoleFunCode += treeNode.Value + ",";
            }

            //删除最后一个逗号
            model.RoleFunCode = StringPlus.DelLastComma(model.RoleFunCode);

            //操作结果
            int dataResult = 0;

            if (DNTRequest.GetQueryString("role_id") != "")
            {
                model.RoleId = Convert.ToInt32(DNTRequest.GetQueryString("role_id").ToString());

                dataResult = new systemRole_BLL().Update(model);
            }
            else
            {
                dataResult = new systemRole_BLL().Add(model);
            }

            if (dataResult >= 1)
            {
                MessageBox.ShowMsgScript(this, "保存成功！", "DG.cancel();ok();");
            }
            else
            {
                MessageBox.Show(this, "保存时发生异常，请重试！");
            }
        }

        #endregion
    }
}